package com.util;

import java.io.File;

import com.alibaba.fastjson.JSONObject;
import com.net.base.BaseRequest;
import com.net.base.BaseResponse;

/**
 * 网络处理工具类
 * 
 * @author 尚业
 *
 */
public class NetUtil {
	// private static Logger logger = Logger.getLogger(LoginFilter.class);

	/**
	 * 获取失败请求响应数据
	 * 
	 * @param resultCode
	 *            响应码
	 * @param message
	 *            失败信息
	 * @return
	 */
	public static <T> BaseResponse<T> getFailuerResponse(int resultCode,
			String message) {
		BaseResponse<T> response = new BaseResponse<T>();
		response.setResultCode(resultCode);
		response.setMessage(message);
		return response;
	}

	/**
	 * 获取失败请求响应数据
	 * 
	 * @param message
	 *            失败信息
	 * @return
	 */
	public static <T> BaseResponse<T> getCommonFailuerResponse(String message) {
		BaseResponse<T> response = new BaseResponse<T>();
		response.setSuccess(false);
		response.setMessage(message);
		return response;
	}

	/**
	 * 获取普通的成功请求响应数据
	 * 
	 * @param message
	 *            失败信息
	 * @return 反馈给客户端的信息
	 */
	public static <T> BaseResponse<T> getCommonSuccessResponse(String message) {
		BaseResponse<T> response = new BaseResponse<T>();
		response.setSuccess(true);
		response.setMessage(message);
		return response;
	}

	/**
	 * 根据数据库返回码，返回服务端数据
	 * 
	 * @param code
	 * @return
	 */
	public static <T> BaseResponse<T> returnResponseByDbCode(int code) {
		if (code != 0 && code != -1) {
			return NetUtil.getCommonSuccessResponse("请求成功");
		} else {
			return NetUtil.getCommonFailuerResponse("请求失败");
		}
	}

	/**
	 * 封装基础的返回的数据
	 * 
	 * @param data
	 * @param message
	 * @return
	 */
	public static <T> BaseResponse<T> getDataResponse(T data, String message) {
		BaseResponse<T> response = new BaseResponse<T>();
		response.setBody(data);
		response.setSuccess(true);
		response.setMessage(message);
		return response;
	}

	/**
	 * 从BaseRequest里获取封装的实体
	 * 
	 * @param request
	 *            请求数据
	 * @param toal
	 *            封装的数据类型
	 * @return
	 */
	public static <T> T getBodyFromBaseRequest(BaseRequest<T> request,
			Class<T> toal) {
		JSONObject jObject = (JSONObject) JSONObject.toJSON(request);
		String jsonString = jObject.get("body").toString();
		T object = JSONObject.parseObject(jsonString, toal);
		return object;
	}

	/**
	 * 根据url删除文件
	 * 
	 * @param url
	 * @return
	 */
	public static boolean deleteFileByUrl(String url) {
		String prefix=Utils.getApplicationProperties().getProperty("applicationHost");
		if (url != null && !url.equals("")) {
			String[] abImage = url.split(prefix);
			String absolutePath = System.getProperty("root") + abImage[1];
			return Utils.deleteAllFilesOfDir(new File(absolutePath));
		}
		return false;
	}

	// public static String getFilePath(String subPath, String
	// fileSuff,InputStream inputStream){
	//
	// String realPath=System.getProperty("root");
	// String
	// host=Utils.getApplicationProperties().getProperty("applicationHost");
	// String newFileName = new Date().getTime() + "" + new Random().nextInt() +
	// fileSuff;
	// realPath =realPath+subPath+newFileName;
	// logger.warn(realPath);
	// File file = new File(realPath+absolutePath);
	// FileUtils.copyInputStreamToFile(headPhotoFile.getInputStream(), file);
	// // String realPath =
	// request.getSession().getServletContext().getRealPath("/");
	// // String
	// host=Utils.getApplicationProperties().getProperty("applicationHost");
	// // String absolutePath = "image/"+userName+"/";
	// // String newFileName = new Date().getTime() + "" + new
	// Random().nextInt() + ".jpg";
	// // absolutePath+=newFileName;
	// // logger.warn(realPath+absolutePath);
	// return null;
	// }
}
